// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Heal a civilian
// -----------------------------------------------

private [
	"_arrayofpos",
	"_buildings",
	"_index",
	"_missioncomplete",
	"_position",
	"_unit"
];

_unit = _this select 0;

_arrayofpos = [];
_missioncomplete = false;

_unit setVariable ["wcprotected", true];
_unit setCaptive true;
_unit allowFleeing 0;
_unit setUnitPos "Up";
doStop _unit;
removeAllWeapons _unit;

_buildings = nearestObjects [position _unit, ["House"], 350];
{
	if (getDammage _x == 0) then {
		_index = 0;
		while {format ["%1", _x buildingPos _index] != "[0,0,0]"} do {
			_position = _x buildingPos _index;
			_arrayofpos = _arrayofpos + [_position];
			_index = _index + 1;
			sleep 0.05;
		};
	};
} foreach _buildings;

_position = _arrayofpos call BIS_fnc_selectRandom;

_unit setPos _position;
_unit setDamage 0.9;

_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
_unit setVariable ["wchostage", true, true];

wcgarbage = [_unit] spawn {
	private ["_unit"];
	_unit = _this select 0;
	while {((alive _unit) and (_unit getVariable "wchostage"))} do {
		wchostage = _unit;
		["wchostage", "client"] call WC_fnc_publicvariable;
		sleep (5 + random 15);
	};
};

while {!_missioncomplete} do {
	if(!alive _unit) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONFAILED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
	};
	if (getDammage _unit < 0.1) then {
		_unit setVariable ["wchostage", false, true];
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMPLETED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 1;
	};
	sleep 1;
};

sleep 120;

_unit setDamage 1;
deleteVehicle _unit;